<script language="javascript" runat="server">

function convertDateStringToMinutes(dateTime) // dateTime as YYYY-MM-DD hh:mm:ss
{
	dtm = restoreDate(dateTime);
	return convertDateToMinutes(dtm);
}

function convertDateToMinutes(dtm)
{
	// Number of  seconds since 1970 (min date)
	// Always returns an integer because milliseconds are set to 0
	dtm.setMilliseconds(0);
	return (dtm.valueOf() / 1000);
}

function convertMinutesToDateString(minutes)
{
	var dtm = convertMinutesToDate(minutes);
	
	return formatDate(dtm)
}

function formatDate(dtm)
{
	var dateString = new String("");
	
	var monthString = ("00" + (dtm.getMonth() + 1));
	var dayString = ("00" + (dtm.getDay()));
	var hourString = ("00" + (dtm.getHours()));
	var minuteString = ("00" + (dtm.getMinutes()));
	var secondString = ("00" + (dtm.getSeconds()));

	dateString += dtm.getFullYear();
	dateString += "-" + monthString.substr(monthString.length-2,2);
	dateString += "-" + dayString.substr(dayString.length-2,2);
	dateString += " " + hourString.substr(hourString.length-2,2);
	dateString += ":" + minuteString.substr(minuteString.length-2,2);
	dateString += ":" + secondString.substr(secondString.length-2,2);
	
	return dateString;
}

function restoreDate(dateTime)
{
	var dateString = new String(dateTime);
	
	//Check the format: YYYY-MM-DD hh:mm:ss
	if(!dateString.match("^[0-9]{4}[\\-]{1}[0-1]{1}[0-9]{1}[\\-]{1}[0-3]{1}[0-9]{1}[\\s]{1}[0-2]{1}[0-9]{1}[:]{1}[0-5]{1}[0-9]{1}[:]{1}[0-5]{1}[0-9]{1}$"))
	{
		DisplayException(5, new Array(dateString, "YYYY-MM-DD hh:mm:ss"))
		Response.End;
	}
	
	var YYYY = trimBeginningZerosAndReturnInteger(dateString.substr(0, 4));
	var MM = trimBeginningZerosAndReturnInteger(dateString.substr(5, 2));
	var DD = trimBeginningZerosAndReturnInteger(dateString.substr(8, 2));
	var hh = trimBeginningZerosAndReturnInteger(dateString.substr(11, 2));
	var mm = trimBeginningZerosAndReturnInteger(dateString.substr(14, 2));
	var ss = trimBeginningZerosAndReturnInteger(dateString.substr(17, 2));
	
	if(!isValidDate(YYYY, MM, DD, hh, mm, ss))
	{
		DisplayException(6, new Array(dateString));
		Response.End;
	}
	
	var dtm = new Date();
	// JavaScript Month starts from 0, 0 is January, 11 is December
	dtm.setFullYear(YYYY, MM-1, DD);
	dtm.setHours(hh, mm, ss, 0);
	return dtm;
	
}

function convertMinutesToDate(minutes)
{
	return new Date(minutes * 1000);
}

function convertMinutesToDisplayDate(minutes)
{
	var displayDate = new Date(convertMinutesToDate(minutes));
	return displayDate.toLocaleDateString() + " " + displayDate.toLocaleTimeString();
}

function trimBeginningZerosAndReturnInteger(value)
{
	var strValue = new String(value);
	while(strValue.substr(0, 1) == "0" && strValue.length > 1)
	{
		strValue = strValue.substr(1, strValue.length - 1);
	}
	return parseInt(strValue);
}

function isValidDate(Y, M, D, h, m, s)
{
	// maximum number of days in months
	var mDays = new Array(0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	if(M<1 || D<1 || h<0 || m<0 || s<0 || M>12 || D>31 || h>23 || m>59 || s>59)
	{
		return false;
	}
	var yearBy4 = parseInt(Y / 4);
	var yearBy100 = parseInt(Y / 100);
	var isLongYear = (yearBy4==(Y/4) && yearBy100 != (Y/100));
	
	if(D>mDays[M])
	{
		return false;
	}
	
	if(M==2 && !isLongYear && D>28)
	{
		return false;
	}
	
	return true;
}

function dateTimeSelectionToMinutes(groupID)
{
	var rDay = Request.Form.Item(groupID + "_day");
	var rMonth = Request.Form.Item(groupID + "_month");
	var rYear = Request.Form.Item(groupID + "_year");
	var rHour = Request.Form.Item(groupID + "_hour");
	var rMinute = Request.Form.Item(groupID + "_minute");
		
	var dateString = new String(rYear + "-" + rMonth + "-" + rDay + " " + rHour + ":" + rMinute + ":00");
	
	return convertDateStringToMinutes(dateString);
}

</script>